package com.spera.genutil;

import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.spring.api.web.model.SendPhoneMsgLog;
import com.spring.security.core.util.UDate;

public class SpUtil {
	
	/** 
	 * 删除单个文件 
	 * @param   sPath    被删除文件的文件名 
	 * @return 单个文件删除成功返回true，否则返回false 
	 */  
	public static boolean deleteFile(String sPath) {  
	    boolean flag = false;  
	    File file = new File(sPath);  
	    // 路径为文件且不为空则进行删除  
	    if (file.isFile() && file.exists()) {  
	        file.delete();  
	        flag = true;  
	    }  
	    return flag;  
	} 
	
	/**
	 * 新增短信日志
	 * @param mongoService
	 * @param status
	 * @param phone
	 * @param valnum
	 * @param content
	 */
	public static void setPhoneLog(MongoTemplate mongoService,String status,String phone,String valnum,String content){
		SendPhoneMsgLog spm= null;
		try{//记录发送日志
			spm = new SendPhoneMsgLog().setSendTime(UDate.getDate("yyyy-MM-dd HH:mm:ss"))
					.setStatus(status)
					.setPhoneNum(phone)
					.setValNum(valnum)
					.setContent(content);
			
		}catch(Exception e){
			e.printStackTrace();
			spm = new SendPhoneMsgLog().setSendTime(UDate.getDate("yyyy-MM-dd HH:mm:ss"))
				.setStatus("0")
				.setPhoneNum(phone)
				.setValNum(valnum)
				.setContent(content).setErrorlog(e.getMessage().substring(0,e.getMessage().length()>200?200:e.getMessage().length()));
		}finally{
			mongoService.insert(spm);
		}
	}
	
//	public static Map<String, Object> sendPhone(JedisCache jedisCache){
//		int[] arrNum = ApplicationUtils.randomArray(0, 9, 6);
//		String pwd_valnum = String.valueOf(arrNum[0])+
//				String.valueOf(arrNum[1])+
//				String.valueOf(arrNum[2])+
//				String.valueOf(arrNum[3])+
//				String.valueOf(arrNum[4])+
//				String.valueOf(arrNum[5]);
//		jedisCache.rpush(pwd_token, token);
//		jedisCache.rpush(pwd_token, pwd_valnum);
//		jedisCache.expire(pwd_token, 180);
//		Ihuyi info = new Ihuyi();
//		info.setMobile(au.getPhone());
//		String content = "您的验证码是：【"+pwd_valnum+"】。请不要把验证码泄露给其他人。";
//		info.setContent(content);
//		Status st = ihuyiServiceImpl.sendSms(info);
////		Status st = new Status();
////		st.setStatus("0");
//		if("0".equalsIgnoreCase(st.getStatus())){
//			s.put("pwd_token", pwd_token);
////			s.put("pwd_valnum", pwd_valnum);
//			s.put(ApplicationUtils.STATUS, "1");
//			s.put(ApplicationUtils.MSG, "验证码发送成功");
//			
//			//短信日志
//			SpUtil.setPhoneLog(mongoService,"1",au.getPhone(),pwd_valnum,content);
//			
//			return s;
//		}else{
//			s.put(ApplicationUtils.STATUS, "0");
//			s.put(ApplicationUtils.MSG, "验证码发送失败");
//			
//			//短信日志
//			SpUtil.setPhoneLog(mongoService,"0",au.getPhone(),pwd_valnum,content);
//			
//			return s;
//		}
//	}
	
	public static String getSessionId(){
		RequestAttributes ra = RequestContextHolder.getRequestAttributes();  
	    ServletRequestAttributes sra = (ServletRequestAttributes)ra;  
	    HttpServletRequest request = sra.getRequest();
	    return request.getSession().getId();
	}
	
	/**
	 * 验证Email
	 * 
	 * @param email
	 */
	public static boolean validateEmail(String email) {
		String regex = "^(\\w-*\\.*)+@(\\w-?)+(\\.\\w{2,})+$";
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(email);
		return matcher.matches();
	}

	/**
	 * 验证手机号
	 * 
	 * @param phoneNum
	 */
	public static boolean validatePhone(String phoneNum) {
		Pattern pattern = Pattern
				.compile("^(13[0-9]|14[0-9]|15[0-9]|17[0-9]|(18[0-9]))\\d{8}$");
		
		
		Matcher matcher = pattern.matcher(phoneNum);
		return matcher.matches();
	}

	/**
	 * chekc正则
	 */
	public static boolean match(String str, String pat) {
		Pattern pattern = Pattern.compile(pat);
		Matcher matcher = pattern.matcher(str);
		return matcher.matches();
	}

}
